/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { Popover, Space, Tag } from 'antd';
import type { ProColumns } from '@ant-design/pro-table';
import { importFieldHide } from '@/utils/tools';
import type {
  JDKITEM,
  LEAGUE_COLUMNS,
  REGISTER_COLUMNS,
  SHIELE_COLUMNS,
  WHITELIST_COLUMNS,
  CHAIN_COLUMNS,
} from '.';
import { getAddressList, getFormatAddress } from './addLeague/_utils';
import { isArray } from 'lodash';

const leagueLevelOption = ['省级', '市级', '区县', '乡镇', '其他'];

const getLeagueOptions = () => {
  const options = leagueLevelOption.map((item, index) => ({
    label: item,
    value: index,
  }));
  return options;
};
const columns: ProColumns<JDKITEM>[] = [
  {
    title: '序号',
    dataIndex: 'id',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 80,
  },
  {
    title: '提交时间',
    dataIndex: 'created_at',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 180,
  },
  {
    title: '同城市场资源',
    dataIndex: 'resources',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 120,
  },
  {
    title: '意向区域',
    dataIndex: 'address',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 150,
  },
  {
    title: '业务员数量',
    dataIndex: 'salesman',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 120,
  },
  {
    title: '姓名',
    dataIndex: 'name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 120,
  },
  {
    title: '联系方式',
    dataIndex: 'phone',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 140,
  },
  {
    title: '留言',
    dataIndex: 'message',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
  },
];

const leagueColumns: ProColumns<LEAGUE_COLUMNS>[] = [
  {
    title: '序号',
    dataIndex: 'id',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 80,
  },
  {
    title: '服务商名称',
    dataIndex: 'league_name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 180,
    render: (_, record) => {
      const { league_name, shunfen } = record;
      return (
        <Space direction='vertical'>
          <span>{league_name}</span>
          {shunfen ? <Tag color='#ff8e2d'>顺丰独占</Tag> : ''}
        </Space>
      );
    },
  },
  {
    title: '账号',
    dataIndex: 'phone',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 120,
  },
  {
    title: '联系电话',
    dataIndex: 'contact_phone',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 140,
  },
  {
    title: '联系人',
    dataIndex: 'contact_name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
  },
  {
    title: '下属商家',
    dataIndex: 'user_count',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 80,
  },
  {
    title: '等级',
    dataIndex: 'level',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
    render: (_, record) => {
      return leagueLevelOption[record.level];
    },
  },
  {
    title: '区域信息',
    dataIndex: 'agent_area_name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
    render: (_, record) => {
      const { agent_area_name } = record;
      const list = getAddressList(agent_area_name);
      return (
        <Popover
          placement='left'
          content={list.map((item, index) => (
            <div key={index}>{item}</div>
          ))}
          title=''
          trigger='hover'
        >
          {list.join('，')}
        </Popover>
      );
      return;
    },
  },
  {
    title: '公司地址',
    dataIndex: '-', //'agent_area_name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
    render: (_, recode) => {
      const { province = '', city = '', district = '', town = '', detail } = recode;
      return province + city + district + town + detail;
    },
  },
  {
    title: '添加时间',
    dataIndex: 'created_at',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
  },
  {
    title: '操作人',
    dataIndex: 'operator',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
  },
];

const chainColumns: ProColumns<CHAIN_COLUMNS>[] = [
  {
    title: '序号',
    dataIndex: 'id',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 80,
  },
  {
    title: '连锁商名称',
    dataIndex: 'name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 180,
  },
  {
    title: '账号',
    dataIndex: 'phone',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 120,
  },
  {
    title: '联系电话',
    dataIndex: 'contact_phone',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 140,
  },
  {
    title: '联系人',
    dataIndex: 'contact_name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
  },
  {
    title: '下属连锁店',
    dataIndex: 'sonCount',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 80,
    render: (_, record) => {
      const { sonCount, phone } = record;
      return (
        <Popover placement='top' content={<div>聚单客账号：{phone}</div>} title='' trigger='hover'>
          {sonCount}
        </Popover>
      );
      return;
    },
  },
  {
    title: '公司地址',
    dataIndex: '-', //'agent_area_name',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
    render: (_, recode) => {
      const { province = '', city = '', district = '', town = '', detail } = recode;
      return province + city + district + town + detail;
    },
  },
  {
    title: '添加时间',
    dataIndex: 'created_at',
    search: false,
    align: 'center',
    ellipsis: true,
    width: 160,
  },
];

const deliveryCountOptions = [
  {
    label: '昨日',
    value: 'day_1',
  },
  {
    label: '近7天',
    value: 'day_7',
  },
  {
    label: '近30天',
    value: 'day_30',
  },
];
const issuedYesterdayOptions = [
  {
    label: '全部',
    value: '',
  },
  {
    label: '发单',
    value: '1',
  },
  {
    label: '未发单',
    value: '2',
  },
];
const registerColumns = () => {
  const columnsList: ProColumns<REGISTER_COLUMNS>[] = [
    {
      title: '序号',
      dataIndex: 'id',
      width: 80,
      search: false,
      align: 'center',
      ellipsis: true,
    },
    {
      title: '账号',
      dataIndex: 'phone',
      width: 100,
      search: false,
      align: 'center',
      render: (_, record) => {
        const { phone, shunfen } = record;
        return (
          <Space direction='vertical'>
            <Popover trigger='hover' content={phone}>
              {importFieldHide(phone, 3, 7)}
            </Popover>
            {shunfen ? <Tag color='#ff8e2d'>顺丰独占</Tag> : ''}
          </Space>
        );
      },
    },
    {
      title: '来源',
      dataIndex: 'league_source_name',
      width: 60,
      search: false,
      align: 'center',
      ellipsis: true,
    },
    {
      title: '注册时间',
      dataIndex: 'created_at',
      width: 100,
      search: false,
      align: 'center',
      ellipsis: true,
    },
    {
      title: '所属服务商及归属员工/连锁商',
      dataIndex: 'league_name',
      width: 160,
      search: false,
      align: 'center',
      render: (_, record) => {
        const { league_name, chain_name, staff_name } = record;
        return league_name || staff_name ? (
          <Space direction='vertical'>
            {league_name && <span>服务商：{league_name}</span>}
            {staff_name && <span>员工：{staff_name}</span>}
            {chain_name && <span>连锁商：{chain_name}</span>}
          </Space>
        ) : (
          '-'
        );
      },
    },
    {
      title: '商家地址',
      dataIndex: 'league_name',
      width: 120,
      search: false,
      align: 'center',
      render: (_, record) => {
        const { shop_address_list } = record;
        return (
          <Popover
            trigger='hover'
            content={
              shop_address_list[0] &&
              shop_address_list.map((item) => {
                return <div key={item}>{item}</div>;
              })
            }
          >
            {shop_address_list[0] ? shop_address_list[0] : '-'}
          </Popover>
        );
      },
    },
    {
      title: '店铺数',
      dataIndex: 'shop_count',
      width: 80,
      search: false,
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        const { shop_count, shop_list } = record;
        return (
          <Popover
            trigger='hover'
            content={
              isArray(shop_list) &&
              shop_list.length > 0 &&
              shop_list.map((item) => {
                return <div key={item.shop_name}>{item.shop_name}</div>;
              })
            }
          >
            {shop_count}
          </Popover>
        );
      },
    },
    {
      title: '发单数',
      dataIndex: 'delivery_count',
      width: 100,
      search: false,
      align: 'center',
      ellipsis: true,
    },
    {
      title: '昨日尝试发单数',
      dataIndex: 'try_delivery_count',
      width: 80,
      search: false,
      align: 'center',
      ellipsis: true,
    },
  ];
  return columnsList;
};

const customersColumns: ProColumns<any>[] = [
  {
    title: '序号',
    dataIndex: 'id',
    width: 50,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '账号',
    dataIndex: 'phone',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '商家名称',
    dataIndex: 'nickname',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '商家来源',
    dataIndex: 'league_source',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
    renderText: (_, record) => {
      const { league_source = '' } = record;
      const arr = {
        '0': '无加盟商',
        '1': '扫码',
        '2': 'admin后台添加',
      };
      return arr[league_source];
    },
  },
  {
    title: '注册时间',
    dataIndex: 'created_at',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '所属服务商',
    dataIndex: 'league_name',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '商家地址',
    dataIndex: 'address_detail',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '店铺数',
    dataIndex: 'shop_count',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '昨日发单数',
    dataIndex: 'delivery_count',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '昨日裹小递发单数',
    dataIndex: 'gxd_delivery_count',
    width: 80,
    search: false,
    align: 'center',
    ellipsis: true,
  },
];

const shieldColumns: ProColumns<SHIELE_COLUMNS>[] = [
  {
    title: '屏蔽运力',
    dataIndex: 'brand_name',
    align: 'center',
    ellipsis: true,
  },
  {
    title: '运力类型',
    dataIndex: 'limit_type',
    align: 'center',
    ellipsis: true,
    valueEnum: {
      1: '平台运力',
      2: '自运力',
      3: '平台运力+自运力',
    },
  },
  {
    title: '屏蔽区域/服务商',
    align: 'center',
    dataIndex: 'area_name_list',
    ellipsis: true,
    render: (_, record) => {
      const { area_name_list, league_name } = record;
      if (league_name) return league_name;
      const list = getFormatAddress(area_name_list);
      return list.length > 0 ? list.join(',') : '全国';
    },
  },
  {
    title: '原因',
    dataIndex: 'remark',
    align: 'center',
    ellipsis: true,
  },
  {
    title: '设置时间',
    dataIndex: 'created_at',
    align: 'center',
    ellipsis: true,
  },
  {
    title: '设置人',
    dataIndex: 'operator',
    align: 'center',
    ellipsis: true,
  },
];

const whiteListColumns: ProColumns<WHITELIST_COLUMNS>[] = [
  {
    title: '商家账号',
    dataIndex: 'phone',
    align: 'center',
    ellipsis: true,
  },
  {
    title: '运力类型',
    dataIndex: 'use_type',
    align: 'center',
    ellipsis: true,
    valueEnum: {
      1: '平台运力',
      2: '自运力',
    },
  },
  {
    title: '运力名称',
    dataIndex: 'brand_name',
    align: 'center',
    search: false,
    ellipsis: true,
  },
  {
    title: '区域/服务商',
    dataIndex: 'area_name_list',
    align: 'center',
    search: false,
    ellipsis: true,
    render: (_, record) => {
      const { area_name_list, league_name } = record;
      if (league_name) return league_name;
      const list = getFormatAddress(area_name_list);
      return list.length > 0 ? list.join(',') : '全国';
    },
  },
];

const sourceOptions = [
  {
    label: '全部',
    value: '',
  },
  {
    label: '直属',
    value: 'reg',
  },
  {
    label: '扫码',
    value: 'scan',
  },
  {
    label: '开放平台',
    value: 'open',
  },
  {
    label: 'admin后台',
    value: 'admin',
  },
];

export {
  columns,
  leagueColumns,
  chainColumns,
  leagueLevelOption,
  getLeagueOptions,
  deliveryCountOptions,
  registerColumns,
  issuedYesterdayOptions,
  sourceOptions,
  customersColumns,
  shieldColumns,
  whiteListColumns,
};
